<template>
  <div class="profile-container">
    <div class="header-card">
      <h2 class="header-title">个人信息</h2>
      <div class="header-content">
        <p class="header-description">您的账户详细信息</p>
      </div>
    </div>

    <div class="profile-content">
      <div class="profile-card">
        <div class="card-content">
          <div class="profile-header">
            <div class="avatar-section">
              <div class="avatar">
                {{ userInfo.fullName?.charAt(0) || userInfo.username?.charAt(0) }}
              </div>
              <h3 class="user-name">{{ userInfo.fullName }}</h3>
              <span class="role-badge">{{ formatRole(userInfo.role) }}</span>
            </div>
          </div>

          <div class="info-grid">
            <div class="info-item">
              <span class="info-label">用户名</span>
              <span class="info-value">{{ userInfo.username }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">手机号码</span>
              <span class="info-value">{{ userInfo.phone }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">邮箱</span>
              <span class="info-value">{{ userInfo.email }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">公司</span>
              <span class="info-value">{{ userInfo.companyName }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">注册时间</span>
              <span class="info-value">{{ formatDateTime(userInfo.createdAt) }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">账号状态</span>
              <span class="info-value status-badge" :class="getStatusClass(userInfo.isActive)">
                {{ userInfo.isActive ? '正常' : '已禁用' }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'

const userInfo = ref({})
const authStore = useAuthStore()

onMounted(async () => {
  await fetchUserInfo()
})

const fetchUserInfo = async () => {
  const userId = authStore.user?.userId
  if (!userId) return

  try {
    const response = await axios.get(`http://localhost:8080/api/users/${userId}`)
    userInfo.value = response.data
  } catch (error) {
    ElMessage.error('获取用户信息失败')
  }
}

const formatRole = (role) => {
  const roleMap = {
    Customer: '客户',
    Employee: '员工',
    Admin: '管理员',
  }
  return roleMap[role] || role
}

const formatDateTime = (dateString) => {
  const date = new Date(dateString)
  return `${date.getFullYear()}年${
    date.getMonth() + 1
  }月${date.getDate()}日 ${date.getHours()}:${date.getMinutes().toString().padStart(2, '0')}`
}

const getStatusClass = (isActive) => {
  return isActive ? 'status-active' : 'status-inactive'
}
</script>

<style scoped>
.profile-container {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.header-card {
  background: white;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  border-radius: 0.5rem;
  padding: 1.5rem;
}

.header-title {
  font-size: 1.125rem;
  font-weight: 600;
  color: #1f2937;
  margin-bottom: 1rem;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-description {
  color: #4b5563;
}

.profile-content {
  background-color: #e3f2fd;
  padding: 1.5rem;
  border-radius: 0.75rem;
}

.profile-card {
  background: white;
  border-radius: 0.75rem;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.card-content {
  padding: 2rem;
}

.profile-header {
  margin-bottom: 2rem;
}

.avatar-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.avatar {
  width: 80px;
  height: 80px;
  background: #2563eb;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  font-weight: 600;
}

.user-name {
  font-size: 1.5rem;
  font-weight: 600;
  color: #1f2937;
}

.role-badge {
  padding: 0.25rem 1rem;
  background-color: #dbeafe;
  color: #1e40af;
  border-radius: 9999px;
  font-size: 0.875rem;
  font-weight: 500;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 2rem;
  margin-top: 2rem;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.info-label {
  font-size: 0.875rem;
  color: #6b7280;
  font-weight: 500;
}

.info-value {
  font-size: 1rem;
  color: #1f2937;
  font-weight: 500;
}

.status-badge {
  display: inline-block;
  padding: 0.25rem 0.75rem;
  border-radius: 9999px;
  font-size: 0.875rem;
  font-weight: 600;
}

.status-active {
  background-color: #d1fae5;
  color: #065f46;
}

.status-inactive {
  background-color: #fee2e2;
  color: #991b1b;
}

@media (max-width: 640px) {
  .info-grid {
    grid-template-columns: 1fr;
  }
}
</style>